Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
debakarr
GitHub Repository: debakarr/machinelearning
Path: blob/master/Part 8 - Deep Learning/Artificial Neural Networks/[R] Artificial Neural Network.ipynb
1009 views
Kernel: R

Artificial Neural Network


Like in Python, we will not have to use our own resource to train the ANN. Here we will use h2o package which connect to a server and train the ANN on that. More importantly h2o have parameter tunning feature which automatically tune the parameter as per requirement.


Data preprocessing

# Importing the dataset dataset = read.csv('Churn_Modelling.csv')
head(dataset, 5)
dataset = dataset[4:14] dataset = dataset[-c(3)]
head(dataset, 5)
# Encoding the categorical variables as factors dataset$Geography = as.numeric(factor(dataset$Geography, levels = c('France', 'Spain', 'Germany'), labels = c(1, 2, 3)))
# Splitting the dataset into the Training set and Test set # install.packages('caTools') library(caTools) set.seed(1234) split = sample.split(dataset$Exited, SplitRatio = 0.8) training_set = subset(dataset, split == TRUE) test_set = subset(dataset, split == FALSE)
# Feature Scaling training_set[-10] = scale(training_set[-10]) test_set[-10] = scale(test_set[-10])
head(training_set, 10)
head(test_set, 10)

Fiting ANN to the Training set

# install.packages('h2o')
library(h2o) h2o.init(nthreads = -1)
---------------------------------------------------------------------- Your next step is to start H2O: > h2o.init() For H2O package documentation, ask for help: > ??h2o After starting H2O, you can use the Web UI at http://localhost:54321 For more information visit http://docs.h2o.ai ---------------------------------------------------------------------- Attaching package: ‘h2o’ The following objects are masked from ‘package:stats’: cor, sd, var The following objects are masked from ‘package:base’: &&, %*%, %in%, ||, apply, as.factor, as.numeric, colnames, colnames<-, ifelse, is.character, is.factor, is.numeric, log, log10, log1p, log2, round, signif, trunc
H2O is not running yet, starting it now... Note: In case of errors look at the following log files: /tmp/Rtmp8aN7Ch/h2o_baka_started_from_r.out /tmp/Rtmp8aN7Ch/h2o_baka_started_from_r.err Starting H2O JVM and connecting: ....... Connection successful! R is connected to the H2O cluster: H2O cluster uptime: 5 seconds 838 milliseconds H2O cluster version: 3.16.0.2 H2O cluster version age: 27 days H2O cluster name: H2O_started_from_R_baka_fmq912 H2O cluster total nodes: 1 H2O cluster total memory: 0.82 GB H2O cluster total cores: 4 H2O cluster allowed cores: 4 H2O cluster healthy: TRUE H2O Connection ip: localhost H2O Connection port: 54321 H2O Connection proxy: NA H2O Internal Security: FALSE H2O API Extensions: XGBoost, Algos, AutoML, Core V3, Core V4 R Version: R version 3.4.2 (2017-09-28)
classifier = h2o.deeplearning(y = 'Exited', training_frame = as.h2o(training_set), activation = 'Rectifier', hidden = c(6, 6), epochs = 100, train_samples_per_iteration = -2)
|======================================================================| 100% |======================================================================| 100%

Predicting the Test set results

prob_pred = h2o.predict(classifier, newdata = as.h2o(test_set[-10])) y_pred = (prob_pred > 0.5) # Convert h2o object to vector y_pred = as.vector(y_pred)
|======================================================================| 100% |======================================================================| 100%
head(y_pred, 20)
head(test_set[, 10], 20)

Making the Confusion Matrix

cm = table(test_set[, 10], y_pred) cm
y_pred 0 1 0 1530 63 1 218 189

Calculating Accuracy

(cm[1, 1] + cm[2, 2]) / 2000

Disconnect from server

h2o.shutdown()
Are you sure you want to shutdown the H2O instance running at http://localhost:54321/ (Y/N)? y